package com.vint.mi;


/**
 * 1.done
 *  乱序数组交换成正序，最少次数
 */
public class Mi22 {
    public static String solution(String line){
        long m = Long.parseLong(line.trim());
        long n = calN(m);
        long sub = m - n * (n + 1)/2;
        if(sub==0){
            return String.valueOf(getKth(n,n));
        }else{
            return String.valueOf(getKth(n+1,sub));
        }
    }


    // (n+1)*n/2 <= m
    public static long calN(long m){
       double ret = 2.0 * m + 0.25;
       return (long)Math.floor(Math.sqrt(ret)-0.5);
    }

    //123456789876...中的第k个，k<n
    public static long getKth(long n, long k){
        long sub = k % 16;
        if(sub<=9&&sub>=1){
            return sub;
        } else if(sub==0) {
            return 2;
        }else{
            return 18-sub;
        }
    }
    public static void main(String[] args) {
        //System.out.println(solution("1"));
        //System.out.println(solution("6"));
        //System.out.println(solution("123456789"));
        System.out.println(solution("3123123"));
    }
}
